package com.zxl.permission.controller;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.zxl.permission.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 登录管理
 */
@RestController
public class LoginController {
    @Autowired
    private LoginService loginService;

    @NoLogin    //无需登录即放行
    @RequestMapping(value = "/login")
    @ResponseBody
    public String login(@RequestBody Map<String, Object> body, HttpServletResponse rsp) {
        String username = (String) body.get("username");
        String password = (String) body.get("password");
        JSONObject res = loginService.login(username, password, rsp);
        return res.toJSONString();
    }

    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    @ResponseBody
    public String logout(@RequestBody Map<String, Object> body, @CookieValue(value = "token") String token, HttpServletResponse rsp) {
        String username = (String) body.get("username");
        JSONObject res = loginService.logout(username, token, rsp);
        return res.toJSONString();
    }

    @RequestMapping(value = "/test", method = RequestMethod.POST)
    @ResponseBody
    public String test(@RequestBody Map<String, Object> body) {
        return "{123}";
    }


//    @RequestMapping(value = "/register", method = RequestMethod.POST)
//    @ResponseBody
//    public Map<String, Object> register(@RequestBody Map<String, Object> req)) {
//        Map<String, Object> map = new HashMap<>();
//
//
//        System.out.println('1');
//        map.put("msg", "success");
//        map.put("username", username);
//        map.put("password", password);
//        return map;
//    }
}
